package com.wsd.smartcarwasher.controller;


import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.Activity;
import com.wsd.smartcarwasher.domain.Pagination;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "活动相关（新增）")
@Controller
@CrossOrigin
public class ActivityController {

    @Resource
    private com.wsd.smartcarwasher.service.ActivityService  activityService;
    @Resource
    private WxService wxService;

    @ApiOperation(value="--获取最新活动 ", notes = "获取最新活动", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/getNewActivity", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getNewActivity(){
        List<Activity> ls =activityService.getNewActivity();
        ls.stream().filter(activity -> activity.geteTime().before(new Date())).collect(Collectors.toList()).forEach(activity -> activity.setStatus(1));
        if(ls==null ||ls.size() ==0){
            return ReturnUtil.Success("暂无内容");
        }
        return ReturnUtil.Success("获取成功",ls);
    }

    @Log("添加最新活动")
    @ApiOperation(value="添加最新活动 ", notes = "获取最新活动", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/createActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap createActivity(@RequestBody Activity activity){
        activity.setcTime(new Date());
        int ls =activityService.createActivity(activity);
        if(ls==0){
            return ReturnUtil.Error("添加失败");
        }
        return ReturnUtil.Success("添加成功");
    }

    @Log("修改活动")
    @ApiOperation(value="修改活动", notes = "获取最新活动", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/activity/changeStatus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap changeStatus(@RequestBody @ApiParam Activity activity){
        int ls =activityService.changeStatus(activity);
        if(ls==0){
            return ReturnUtil.Error("修改失败");
        }
        return ReturnUtil.Success("修改成功");
    }

    @Log("删除了活动")
    @ApiOperation(value="删除了活动 ", notes = "获取最新活动", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/activity/delete", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap deleteActivity(@RequestParam int id){
        int rows=activityService.deleteActivity(id);
        if(rows<1){
            return ReturnUtil.Error("删除失败");
        }
        return ReturnUtil.Success("删除成功");
    }

    /**
     * 首页展示随机广告，广告包含2种，会员卡和优惠券,后台设置绑定时，需设置一种广告绑定一种会员卡或者优惠券
     * @return
     */
//    @Log("首页随机展示广告,广告分为2种,一种会员卡，一种优惠券")
    @ApiOperation(value="（新增=>小程序首页）展示广告 ", notes = "随机展示广告", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/activity/show", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap showActivity(HttpServletRequest request){
        String sessionId = request.getHeader("authorization");
        UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());

        }
//        return ReturnUtil.Success(null);
        return ReturnUtil.Success(ResultState.QUERY_ADVERTISEMENT_SUCCESS.getMsg(),this.activityService.showActivity());
    }

    @Log("广告列表展示")
//    @RequiresPermissions("get:activity:list")
    @ApiOperation(value="广告列表展示 ", notes = "随机展示广告", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/listActivity", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap listActivity(@RequestParam("Page") int Page, @RequestParam(value = "PageSize",defaultValue = "10") int PageSize,HttpServletRequest request){

        List<Activity> activityList=  this.activityService.listActivity();
        return ReturnUtil.Success(ResultState.QUERY_ADVERTISEMENT_SUCCESS.getMsg(),new Pagination(Page, PageSize,activityList));
    }

    @Log("设置广告上线/下线")
    @RequestMapping(value = "/v1/setActivityStatus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value="设置广告上线/下线", notes = "设置广告上线/下线", produces = "application/json", response = ModelMap.class)
    @ResponseBody
    public ModelMap listActivity(@RequestBody Activity activity){

        int changes=this.activityService.changeStatus(activity);
        if(changes==0)
        {
            return ReturnUtil.Success("修改失败",activity);
        }
        return ReturnUtil.Success("修改成功",activity);
    }


}
